package com.cl.edu.web.admin;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.cl.common.CrudActionSupport;
import com.cl.core.orm.Page;
import com.cl.edu.entity.News;
import com.cl.edu.entity.Newssort;
import com.cl.edu.service.admin.NewsManager;
 
/**
 * 新闻信息管理
 * @author Administrator
 *
 */
public class NewsAction extends CrudActionSupport<News> {

	@Autowired
	private NewsManager newsManager;
	private Page<News> page = new Page<News>();
	private News entity;
	private Long id;

	private String filter_title;
	private String filter_typeid;

	private List<Newssort> allChannel;

	@Override
	public String list() throws Exception {
		if (!page.isOrderBySetted()) {
			page.setOrderBy("time");
			page.setOrder(Page.DESC);
		}

		// 得到初始化栏目列表
		allChannel = newsManager.getNewssorts();
		// 得到初始化新闻列表
		page = newsManager.getNewsList(page);
		return SUCCESS;
	}

	public News getModel() {
		return entity;
	}

	@Override
	public String input() throws Exception {
		// 得到初始化栏目列表
		allChannel = newsManager.getNewssorts();
		return INPUT;
	}

	/**
	 * 新增新闻
	 */
	@Override
	public String save() throws Exception {
		if (id == null) {
			entity.setReadNum(0);
			entity.setTime(new Date());
			newsManager.saveOrUpdateNews(entity);
			page.setPageNo(1);
			addActionMessage("新增新闻成功");
		} else {
			newsManager.clear();
			entity.setTime(new Date());
			// content.setReleaseDate(new Date());
			newsManager.saveOrUpdateNews(entity);
			addActionMessage("更新新闻成功");
		}

		return list();
	}

	@Override
	public String delete() throws Exception {
		try {
			newsManager.deleteNews(id);
			addActionMessage("删除新闻成功");
		} catch (Exception e) {
			addActionMessage("删除新闻失败");
		}
		return list();
	}

	@Override
	protected void prepareModel() throws Exception {
		if (id == null) {
			entity = new News();
		} else {
			entity = newsManager.getNewsById(id);
		}
	}

	public Page<News> getPage() {
		return page;
	}

	public void setPage(Page<News> page) {
		this.page = page;
	}

	public News getEntity() {
		return entity;
	}

	public void setEntity(News entity) {
		this.entity = entity;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public List<Newssort> getAllChannel() {
		return allChannel;
	}

	public void setAllChannel(List<Newssort> allChannel) {
		this.allChannel = allChannel;
	}

	public String getFilter_title() {
		return filter_title;
	}

	public void setFilter_title(String filter_title) {
		this.filter_title = filter_title;
	}

	public String getFilter_typeid() {
		return filter_typeid;
	}

	public void setFilter_typeid(String filter_typeid) {
		this.filter_typeid = filter_typeid;
	}

}
